package institution

import (
	"net/http"

	"github.com/EDDYCJY/go-gin-example/pkg/app"
	"github.com/EDDYCJY/go-gin-example/pkg/e"
	"github.com/EDDYCJY/go-gin-example/pkg/util"
	"github.com/EDDYCJY/go-gin-example/service/institution_service"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

type GetSampleVerify struct {
}

func GetSample(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form GetSampleVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := institution_service.GetSample{
		InstitutionID: InstitutionID,
		PageNum:       util.GetPage(c),
		PageSize:      util.GetLimit(c),
	}
	data, err := Service.GetSample()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type AddSampleVerify struct {
	GoodsID     int    `alias:"商品ID" form:"goods_id"  json:"goods_id" valid:"Required;"`
	GoodsNum    int    `alias:"商品数量" form:"goods_num"  json:"goods_num" valid:"Required;"`
	ExpressNo   string `alias:"快递单号" form:"express_no"  json:"express_no" `
	ExpressName string `alias:"快递名称" form:"express_name"  json:"express_name" `
	StreamerID  int    `alias:"主播 ID" form:"streamer_id"  json:"streamer_id" valid:"Required;"`
}

func AddSample(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form AddSampleVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	UserID := c.MustGet("user_id").(int)

	Service := institution_service.AddSample{
		InstitutionID: InstitutionID,
		UserID:        UserID,
		StreamerID:    form.StreamerID,
		ExpressName:   form.ExpressName,
		ExpressNo:     form.ExpressNo,
		GoodsID:       form.GoodsID,
		GoodsNum:      form.GoodsNum,
	}
	data, err := Service.AddSample()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type EditSampleVerify struct {
	ID          int    `form:"id" valid:"Required;"`
	ExpressNo   string `alias:"快递单号" form:"express_no"  json:"express_no" `
	ExpressName string `alias:"快递名称" form:"express_name"  json:"express_name" `
}

func EditSample(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form = EditSampleVerify{ID: com.StrTo(c.Param("id")).MustInt()}
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := institution_service.EditSample{
		InstitutionID: InstitutionID,
		SampleID:      form.ID,
		ExpressName:   form.ExpressName,
		ExpressNo:     form.ExpressNo,
	}
	data, err := Service.EditSample()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}
